package com.chen.schedule.service.impl;

import com.chen.schedule.dao.SysUserDao;
import com.chen.schedule.dao.impl.SysUserDaoImpl;
import com.chen.schedule.pojo.SysUser;
import com.chen.schedule.service.SysUserService;
import com.chen.schedule.utils.MD5Util;

import java.util.List;

public class SysUserServiceImpl implements SysUserService {

    private SysUserDao userDao = new SysUserDaoImpl();


    @Override
    public Integer save(SysUser user) {
        // 对数据进行处理
        // 对密码进行加密
        user.setUserPwd(MD5Util.encrypt(user.getUserPwd()));
        // 调用dao层的save方法
        return userDao.save(user);
    }

    @Override
    public Integer login(SysUser user) {
        List<SysUser> users = userDao.queryByUsername(user.getUsername());
//        0 代表用户名不存在 1 代表密码错误 2登录成功
        if (users == null || users.size() == 0) {
            return 0;
        }
        // 用户名存在
        // 校验密码
        // 先加密
        String password = MD5Util.encrypt(user.getUserPwd());
        SysUser queryUser = users.get(0);
        // 校验密码不一致
        if (!queryUser.getUserPwd().equals(password)) {
            return 1;
        }
        return 2;
    }
}
